Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

15.8 Kernel Same-Page Merging

Throughout system operation, the operating system occasionally duplicates memory payloads. For instance, during a process fork cycle, a child process typically copies an extensive portion of its parent's execution state, which dramatically decreases overall memory utilization efficiency. To optimize memory footprint utilization, Linux operates an asynchronous background kernel daemon designated as ksmd. This daemon continuously scans the contents of anonymous memory pages that have explicitly enabled KSM functionality, comparing their payloads against other candidate memory pages. When the contents of a page remain identical to those of another page over an extended duration, the system merges the two independent physical pages into a single frame, enabling all referencing processes to share a singular physical page asset. This optimization paradigm is designated as Kernel Same-page Merging (KSM).

When executing same-page merging operations, the Linux system utilizes two distinct types of binary search trees: the stable tree (stable node tree) and the unstable tree (unstable node tree). The stable tree is structured as a red-black tree woven together via the node field embedded within struct stable_node descriptors. The physical page frames indexed within the stable tree are strictly write-protected, which guarantees that the content of the physical page managed under a stable tree node remains immutable and architecturally legitimate. Throughout a scanning pass, ksmd first evaluates whether the content of a candidate page matches the payload of an existing page frame already indexed under a stable tree node. If a match is found, the system remaps the calling process's page table entries to point directly to the stable tree's physical page frame and subsequently releases the redundant physical page back to the allocator. If the content diverges from the stable tree entries, the daemon computes a checksum to determine whether the page payload undergoes frequent modifications. If the page content remains static over an extended observation window, the system checks whether the unstable tree contains a node whose underlying page payload matches the candidate page. If a matching element is discovered inside the unstable tree, the engine merges the candidate page with the matching page, instantiates a stable tracking node, and relocates the entity out of the unstable tree over into the stable tree. Conversely, if no match is found, a new tracking node representing the current page is inserted into the unstable tree. Because all physical pages tracked within the stable tree are strictly write-protected, any subsequent write transaction attempted by a process against a merged stable page triggers a page table write fault, thereby prompting the system to execute a synchronous Copy-on-Write (CoW) operation to break the sharing link.

The stable tree is structurally represented as a red-black tree composed of two distinct categories of nodes: descriptor nodes and chain nodes, both of which are programmatically described via the stable_node structure. A descriptor node represents a standalone, single node within the stable tree that directly maps to a shared physical page frame. To preserve the structural balance of the red-black stable tree, the maximum length of the tracking list attached to each node—governed by the rmap_hlist_len field—is strictly capped by the system threshold parameter ksm_max_page_sharing. When the total number of virtual pages sharing the physical memory frame indexed by a single node reaches the threshold defined by ksm_max_page_sharing, the system instantiates a specialized node designated as a chain node to act as the head anchor for a hashed linked list. Following this instantiation, the engine replaces the original descriptor node inside the stable tree with this newly created chain node, and appends the original descriptor node onto a hashed linked list referenced by the chain's hlist field. This specific sub-list is threaded together via the hlist_dup field of the stable_node structures, where all attached nodes map to the exact same physical memory payload. Aside from the root chain node itself, the auxiliary nodes attached along this sub-list are designated as duplicated nodes or dup nodes.

Standard descriptor nodes are differentiated from the chain list head nodes (chain nodes) by evaluating the rmap_hlist_len field within the stable_node structure; a chain node sets its rmap_hlist_len field to a negative value. Furthermore, a chain list head is distinguished from its trailing duplicate nodes via the head field of the stable_node structure, as duplicate nodes explicitly populate their head field with the architectural bitmask flag STABLE_NODE_DUP_HEAD.

The hlist field of a standard descriptor node points directly to an reverse-mapping linked list composed of rmap_item entries, whereas the hlist field of a chain node points to a linked list tracking sub-chains of dup nodes. The definitive structural layout of the stable tree framework is illustrated in Figure 15-27.

Figure 15-27 Architectural Layout of the KSM Stable Tree The source code implementing the Kernel Same-page Merging engine is hosted within the file git/mm/ksm.c.

Copyright © 2026 Guangzhou eSmart Information Technology Co., Ltd. All Rights Reserved.
All content in this book is copyrighted by the author. For personal learning and exchange only. Commercial use without authorization is strictly prohibited. Email:

✉ 3348194517@qq.com | hai616878@gmail.com